﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


using KingRoad.EPM.PersonnelLogEntity.Model;
using Unis.Framework.Core.Utility;
using KingRoad.EPM.PersonnelLogDAL.PlmPersonLeaveService;
using KingRoad.EPM.PersonnelLogDAL.PlmPersonnelinfoService;
using KingRoad.EPM.PersonnelLogDAL.ScmSafetyconstructiondicService;


namespace KingRoad.EPM.PersonnelLogBLL.UIRules
{
    /// <summary> 
    /// 客户端业务处理类。 
    /// 文件生成时间 2017/3/17 10:53:34
    /// </summary> 
    public class PlmPersonleaveUIRule
    {
        private readonly IPlmPersonleaveService _plmPersonleaveService;
        private PlmPersonnelinfoUIRule _plmpersonnelinfoUIRule;
        private ScmSafetyconstructiondicUIRule _scmsafetyconstructiondicUIRule;

        public PlmPersonleaveUIRule() { }

        /// <summary> 
        /// construct function... 
        /// </summary> 
        public PlmPersonleaveUIRule(IPlmPersonleaveService plmPersonleaveService)
        {
            _plmPersonleaveService = plmPersonleaveService;
        }

        #region 紫光原始
        /// <summary>
        /// 获取请假记录
        /// </summary>
        /// <param name="name">姓名</param>
        /// <param name="section">所属标段</param>
        /// <param name="time">时间</param>
        /// <returns></returns>
        //public List<PlmPersonleaveInfo> GetListBySql(string name, string section, string time)
        //{
        //    var list = _plmPersonleaveService.GetEntityList("Pl_PersonName='" + name + "' and Pl_Section='" + section + "' and Pl_State=5 and  CONVERT(varchar(120), Pl_Time, 120) LIKE '" + time + "%'");
        //    return list;
        //}
        #endregion

        #region zpp 20170424 修改
        /// <summary>
        /// 获取请假记录
        /// </summary>
        /// <param name="name">姓名</param>
        /// <param name="section">所属标段</param>
        /// <param name="time">时间</param>
        /// <returns></returns>
        public List<PlmPersonleaveInfo> GetListBySql(string name, string section, string time)
        {
            var list = _plmPersonleaveService.GetEntityList("Pl_PersonName='" + name + "' and Pl_Section='" + section + "' and Pl_State=5 and  (( CONVERT(varchar(120), PL_StartDate, 120) LIKE '" + time + "%') or (CONVERT(varchar(120), PL_EndDate, 120) LIKE '" + time + "%'))");
            return list;
        }
        #endregion

        /// <summary>
        /// 根据ID获取Info
        /// </summary>
        /// <param name="parentID">主键ID</param>
        /// <returns></returns>
        public PlmPersonleaveInfo GetInfoByID(Guid ID)
        {
            var list = _plmPersonleaveService.GetEntityById(ID);
            return list;
        }
        /// <summary>
        /// 根据条件查询List信息(分页)
        /// </summary>
        /// <param name="numCurrentPage">当前页</param>
        /// <param name="numPerPage">每页数据大小</param>
        /// <param name="orderByField">排序字段名称</param>
        /// <param name="totalRecord">总数据量</param>
        /// <param name="sqlCondition">sql查询条件</param>
        /// <returns></returns>
        public List<PlmPersonleaveInfo> GetListForPage(int numCurrentPage, int numPerPage,
                                                                        string orderByField, out int totalRecord, string sqlCondition, string desc = "desc")
        {
            var list = _plmPersonleaveService.GetEntityListByContionRe(numCurrentPage, numPerPage, orderByField, out totalRecord, sqlCondition, desc);
            return list;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool InsertEntity(PlmPersonleaveInfo entity)
        {
            //判断是否是主任请假
            _plmpersonnelinfoUIRule = new PlmPersonnelinfoUIRule(new PlmPersonnelinfoService());
            PlmPersonnelinfoInfo pinfo=_plmpersonnelinfoUIRule.GetPersonnelinfoByID(new Guid(entity.Pl_PersonID));
            _scmsafetyconstructiondicUIRule = new ScmSafetyconstructiondicUIRule(new ScmSafetyconstructiondicService());
            ScmSafetyconstructiondicInfo cinfo=_scmsafetyconstructiondicUIRule.GetSafetyconstructiondicByName(pinfo.Pi_Post);
            entity.Pl_Type = false;
            if (cinfo.Scd_Description != null)
            {
                if (cinfo.Scd_Description.IndexOf("主任") >= 0)
                    entity.Pl_Type = true;
            }
            var ret = _plmPersonleaveService.InsertEntity(entity);
            return ret > 0;
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateEntity(PlmPersonleaveInfo entity)
        {
            //判断是否是主任请假
            _plmpersonnelinfoUIRule = new PlmPersonnelinfoUIRule(new PlmPersonnelinfoService());
            PlmPersonnelinfoInfo pinfo = _plmpersonnelinfoUIRule.GetPersonnelinfoByID(new Guid(entity.Pl_PersonID));
            _scmsafetyconstructiondicUIRule = new ScmSafetyconstructiondicUIRule(new ScmSafetyconstructiondicService());
            ScmSafetyconstructiondicInfo cinfo = _scmsafetyconstructiondicUIRule.GetSafetyconstructiondicByName(pinfo.Pi_Post);
            entity.Pl_Type = false;
            if (cinfo.Scd_Description != null)
            {
                if (cinfo.Scd_Description.IndexOf("主任") >= 0)
                    entity.Pl_Type = true;
            }
            var ret = _plmPersonleaveService.UpdateEntity(entity);
            return ret > 0;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteEntity(string id)
        {
            var ret = _plmPersonleaveService.DeleteEntityByStringID(id);
            return ret > 0;
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool DeleteEntityByMultiStringID(string ids)
        {
            var ret = 0;
            foreach (string id in ids.Split(','))
            {
                ret = _plmPersonleaveService.DeleteEntityByStringID(id);
            }
            return ret > 0;
        }
    }
}

